/*
 * JBoss, Home of Professional Open Source Copyright 2011, Red Hat, Inc. and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a full listing of individual
 * contributors. This is free software; you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version. This software is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. You should have received a copy of the GNU Lesser General Public License along
 * with this software; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
 * Floor, Boston, MA 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.grupoconstruobras.sigo.exhumaciones.ui.datamodel;

import java.util.Collections;
import java.util.logging.Logger;

import javax.faces.context.FacesContext;
import javax.persistence.Query;
import javax.persistence.Tuple;
import javax.persistence.TypedQuery;

import org.ajax4jsf.model.DataVisitor;
import org.ajax4jsf.model.Range;
import org.ajax4jsf.model.SequenceRange;
import org.richfaces.model.Arrangeable;

public class CriteriaDataModel extends ExtendedDataModel<Tuple> implements
		Arrangeable {

	private Logger LOG = Logger.getLogger(CriteriaDataModel.class.getName());
	private IDataModelCallback<Tuple> callback;
	
	public CriteriaDataModel(IDataModelCallback<Tuple> callback, IQueryBuilder builder) {
		this(builder);
		this.callback = callback;
	}
	
	public CriteriaDataModel(IQueryBuilder builder) {
		super(builder);
	}
	
	@Override
	public void walk(FacesContext context, DataVisitor visitor, Range range,
			Object argument) {
		if (arrangeableState == null)
			return;
		
		Query q = builder.select();
		if(q == null)
			return;
		
		SequenceRange sequenceRange = (SequenceRange) range;
		if (sequenceRange.getFirstRow() >= 0 && sequenceRange.getRows() > 0) {
			q.setFirstResult(sequenceRange.getFirstRow());
			q.setMaxResults(sequenceRange.getRows());
		}
		
		if(callback != null)
			callback.preQueryExecute(q);
		rows = Collections.unmodifiableList(q.getResultList());
		if(callback != null)
			callback.postQueryExecute(rows);
		int size = rows.size();
		for (int index=0; index < size; index++) {
			visitor.process(context, index, argument);
		}
	}

	@Override
	public int getRowCount() {
		if (arrangeableState == null)
			return 0;
		
		Query q = builder.count();
		
		if (q == null)
			return 0;
		Object result = ((TypedQuery<Tuple>)q).getSingleResult();
		LOG.info("Resultado consulta count: " + result);
		count = ((Number) ((Tuple)result).get(0, Long.class)).intValue();
		return count;
	}

	@Override
	public Tuple getRowData() {
		return rows.get((Integer)rowKey);
	}
}
